package com.ruoyi.quartz.task;

import com.ruoyi.db.domain.MerchantCheckInfo;
import com.ruoyi.laiqianba.service.IAddressInfoService;
import com.ruoyi.laiqianba.service.IAdminHttpService;
import com.ruoyi.merchant.service.IMerchantBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component("merchantCheck2Task")
public class MerchantCheck2Task {

    @Autowired
    IAdminHttpService  adminHttpService;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;
    @Autowired
    IAddressInfoService addressInfoService;

    private static final List<String> phoneList = Collections.unmodifiableList(
            Arrays.asList("13018968065", "19157755364", "13095710536", "13071822506","13073630328","13082809928","16665736239","17606512709"
                    ,"16665738926","13282112936","18032178052","15613347369")
    );

    private static final String FIXED_ID_CARD = "140224199207079734";

    private static final List<String> NAME_POOL = Arrays.asList(
            "孙宇", "钱坤", "周婷", "吴刚", "郑爽", "王磊", "冯军", "陈婷", "褚琳", "卫华",
            "蒋欣", "沈腾", "韩雪", "杨幂", "朱一龙", "秦岚", "尤勇", "许晴", "何炅", "吕良"
    );
    private Set<String> usedNames = new HashSet<>();

    public void merchantCheck2(){



        List<Map<String, String>> infoIdList = merchantBaseInfoService.getChantBaseInfoId();
        List<Map<String, String>> cityList = addressInfoService.getCtiy();

        List<String> cities = new ArrayList<>();

        for (Map<String, String> map : cityList) {
            String city = map.get("value");
            if (city != null && city.trim().endsWith("市")) {
                cities.add(city);
            }
        }

        List<String> infoNames = infoIdList.stream()
                .map(map -> map.get("key"))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        List<String> infoIds = infoIdList.stream()
                .map(map -> {
                    Object value = map.get("value");
                    if (value instanceof Integer) {
                        return String.valueOf((Integer) value);
                    }
                    return (String) value;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        Random random = new Random();
        usedNames.clear();
        for (int i = 12; i < infoIds.size(); i++) {
            MerchantCheckInfo merchantCheckInfo = new MerchantCheckInfo();
            merchantCheckInfo.setInfoId(Long.valueOf(infoIds.get(i)));
            merchantCheckInfo.setInfoName(infoNames.get(i));
            merchantCheckInfo.setMobile(phoneList.get((i - 12) % phoneList.size()));
            merchantCheckInfo.setRealName(getUniqueRandomName(random));
            merchantCheckInfo.setIdCard(FIXED_ID_CARD);
            merchantCheckInfo.setCity(cities.get(random.nextInt(cities.size())));
            adminHttpService.merchantcheckPush(merchantCheckInfo);
        }
    }

    private String getUniqueRandomName(Random random) {
        if (usedNames.size() >= NAME_POOL.size()) {
            usedNames.clear();
        }
        String name;
        int attempts = 0;

        do {
            name = NAME_POOL.get(random.nextInt(NAME_POOL.size()));
            attempts++;
            if (attempts > NAME_POOL.size() * 2) {
                name = name + attempts;
                break;
            }
        } while (usedNames.contains(name));
        usedNames.add(name);
        return name;
    }

}
